package fir.diagram.navigator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import fir.diagram.edit.parts.FIREditPart;
import fir.diagram.edit.parts.FuncionalidadeEditPart;
import fir.diagram.edit.parts.FuncionalidadeLabelEditPart;
import fir.diagram.edit.parts.InformacaoEditPart;
import fir.diagram.edit.parts.InformacaoLabelEditPart;
import fir.diagram.edit.parts.LinkInformacaoRegraEditPart;
import fir.diagram.edit.parts.LinkRegraFuncionalidadeEditPart;
import fir.diagram.edit.parts.LinkRegraInformacaoEditPart;
import fir.diagram.edit.parts.RegraEditPart;
import fir.diagram.edit.parts.RegraLabelEditPart;
import fir.diagram.part.FirDiagramEditorPlugin;
import fir.diagram.part.FirVisualIDRegistry;
import fir.diagram.providers.FirElementTypes;
import fir.diagram.providers.FirParserProvider;

/**
 * @generated
 */
public class FirNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		FirDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		FirDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof FirNavigatorItem
				&& !isOwnView(((FirNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof FirNavigatorGroup) {
			FirNavigatorGroup group = (FirNavigatorGroup) element;
			return FirDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof FirNavigatorItem) {
			FirNavigatorItem navigatorItem = (FirNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (FirVisualIDRegistry.getVisualID(view)) {
		case FIREditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?fir?FIR", FirElementTypes.FIR_1000); //$NON-NLS-1$
		case InformacaoEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?fir?Informacao", FirElementTypes.Informacao_2001); //$NON-NLS-1$
		case FuncionalidadeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?fir?Funcionalidade", FirElementTypes.Funcionalidade_2002); //$NON-NLS-1$
		case RegraEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?fir?Regra", FirElementTypes.Regra_2003); //$NON-NLS-1$
		case LinkRegraFuncionalidadeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?fir?linkRegraFuncionalidade", FirElementTypes.LinkRegraFuncionalidade_4001); //$NON-NLS-1$
		case LinkInformacaoRegraEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?fir?linkInformacaoRegra", FirElementTypes.LinkInformacaoRegra_4002); //$NON-NLS-1$
		case LinkRegraInformacaoEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?fir?linkRegraInformacao", FirElementTypes.LinkRegraInformacao_4003); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = FirDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& FirElementTypes.isKnownElementType(elementType)) {
			image = FirElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof FirNavigatorGroup) {
			FirNavigatorGroup group = (FirNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof FirNavigatorItem) {
			FirNavigatorItem navigatorItem = (FirNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (FirVisualIDRegistry.getVisualID(view)) {
		case FIREditPart.VISUAL_ID:
			return getFIR_1000Text(view);
		case InformacaoEditPart.VISUAL_ID:
			return getInformacao_2001Text(view);
		case FuncionalidadeEditPart.VISUAL_ID:
			return getFuncionalidade_2002Text(view);
		case RegraEditPart.VISUAL_ID:
			return getRegra_2003Text(view);
		case LinkRegraFuncionalidadeEditPart.VISUAL_ID:
			return getLinkRegraFuncionalidade_4001Text(view);
		case LinkInformacaoRegraEditPart.VISUAL_ID:
			return getLinkInformacaoRegra_4002Text(view);
		case LinkRegraInformacaoEditPart.VISUAL_ID:
			return getLinkRegraInformacao_4003Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getFIR_1000Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getInformacao_2001Text(View view) {
		IAdaptable hintAdapter = new FirParserProvider.HintAdapter(
				FirElementTypes.Informacao_2001,
				(view.getElement() != null ? view.getElement() : view),
				FirVisualIDRegistry.getType(InformacaoLabelEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			FirDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getFuncionalidade_2002Text(View view) {
		IAdaptable hintAdapter = new FirParserProvider.HintAdapter(
				FirElementTypes.Funcionalidade_2002,
				(view.getElement() != null ? view.getElement() : view),
				FirVisualIDRegistry
						.getType(FuncionalidadeLabelEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			FirDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getRegra_2003Text(View view) {
		IAdaptable hintAdapter = new FirParserProvider.HintAdapter(
				FirElementTypes.Regra_2003, (view.getElement() != null ? view
						.getElement() : view), FirVisualIDRegistry
						.getType(RegraLabelEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			FirDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getLinkRegraFuncionalidade_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getLinkInformacaoRegra_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getLinkRegraInformacao_4003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return FIREditPart.MODEL_ID
				.equals(FirVisualIDRegistry.getModelID(view));
	}

}
